

Page({
    data: {
        maxTotal: 10,
        formConfig: {
            prop1: {
                options: [
                    { label: 'label1', value: '1111' }, 
                    { label: 'label2', value: '1112' }, 
                    { label: 'label3', value: '1113' }
                ]
            },
            prop3: {
                options: [
                    { label: 'label1', value: 0, max: 10 }, 
                    { label: 'label2', value: 0, max: 10 }, 
                    { label: 'label3', value: 0, max: 10 }
                ],
            },
            checkbox: {
                options: [
                    { label: 'label1', value: '1111' }, 
                    { label: 'label2', value: '1112' }, 
                    { label: 'label3', value: '1113' }
                ]
            },
            calendar: {
                options: [
                    { value: new Date() }, 
                ]
            },
            select: {
                optgroup: {
                    year: {
                        options: [
                            '1',
                            '2',
                            '3',
                        ]
                    },
                    month: {
                        options: [
                            '1',
                            '2',
                            '3',
                        ]
                    },
                    day: {
                        options: [
                            '1',
                            '2',
                            '3',
                        ]
                    }
                }
            },
            prop2: {
                type: 'text'
            },
            number: {
                type: 'number'
            },
            nickname: {
                type: 'nickname'
            }
        },
        model: {
        }
    },

    onLoad() {
        console.log('form onload');
        let newModel = {
            prop1: '1111',
            prop2: 'init str',
            prop3: [2,2,2],
            checkbox: ['1111'],
            number: 1,
            nickname: '美美',
            select: ['2','2','2']
        }
        this.form1 = this.selectComponent('#form1')
        this.form1.setModel(newModel, {
            handle: function(name, newVal, {ctx, defHandle}) {
                if (ctx.data.ele_type  === 'entry-group') {
                    // console.log(name, newVal);
                    let list = ctx.curInputComCtx.getList();
                    let newList = list.map((v, index) => {
                        v[1].value = newVal[index];
                        return v
                    });
                    // console.log(newList);
                    ctx.curInputComCtx.setList(newList)
                    return;
                }
                else {
                    defHandle()
                }
            }
        });
    },

    getFormVal() {
        let model = this.selectComponent('#form1').getModel()
        console.log(model);
    },

    onEntryChange(e) {
        let dataset = e.target.dataset;
        let option =  dataset.option;
        option[1].value = e.detail;
        // console.log('onEntryChange', e, option);
        let formItem = this.selectComponent('#form1_' + dataset.name);
        // console.log(formItem);
        let inputCtx = formItem.getInputCtx();
        let oldTotal = 0;
        
        let list2 = inputCtx.getList()
        list2.forEach(v => {
            if (v[0] === option[0]) {
                oldTotal = oldTotal + e.detail
            } 
            else {
                oldTotal = oldTotal + v[1].value
            }
        })

        let newList = list2.map(v => {
            if (v[0] === option[0]) {
                v = option
            }
            v[1].max = this.data.maxTotal - oldTotal + v[1].value;
            return v
        })

        // option[1].max = this.data.maxTotal - oldTotal
        inputCtx?.setOption(option[0], option[1])
        // console.log(newList);
        inputCtx.setList(newList)
        let values = inputCtx.getValues().map(v => {
            return v.value
        })
        this.form1.setModelByName(dataset.name, values)
    }
})
